package com.onefinger.manager.views.model
{
	import com.onefinger.manager.events.ManagerEvent;
	import com.onefinger.manager.vos.Category;
	import com.onefinger.manager.vos.Item;
	import com.onefinger.manager.vos.Subcategory;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.utils.Timer;
	
	import mx.collections.ArrayCollection;

	public class ItemCRUDModel extends BaseCRUDModel
	{
		
		//--------------------------------------------------------------------
		//    Public Setters and Getters
		//--------------------------------------------------------------------
		
		//.................items......................
		private var _items:ArrayCollection;
		
		[Bindable(event="changeItems")]	
		public function get items():ArrayCollection
		{
			return _items;
		}
		public function set items(value:ArrayCollection):void
		{
			_items = value;
			dispatchEvent( new Event( "changeItems" ) );
		}
		
		//.................subcategories......................
		private var _subcategories:ArrayCollection;
		
		[Bindable(event="changeSubCategories")]	
		public function get subcategories():ArrayCollection
		{
			return _subcategories;
		}
		public function set subcategories(value:ArrayCollection):void
		{
			_subcategories = value;
			dispatchEvent( new Event( "changeSubCategories" ) );
		}
		
		//.................categories......................
		private var _categories:ArrayCollection;
		
		[Bindable(event="changeCategories")]	
		public function get categories():ArrayCollection
		{
			return _categories;
		}
		public function set categories(value:ArrayCollection):void
		{
			_categories = value;
			dispatchEvent( new Event( "changeCategories" ) );
		}
		
		//.................selectedCategory......................
		private var _selectedCategory:Category;
		[Bindable(event="changeSelectedCategory")]	
		public function get selectedCategory():Category
		{
			return _selectedCategory;
		}
		
		public function set selectedCategory(value:Category):void
		{
			_selectedCategory = value;
			dispatchEvent(new Event("changeSelectedCategory"));
		}
		
		//.................selectedSubcategory......................
		private var _selectedItem:Item;
		
		[Bindable(event="changeItem")]
		public function get selectedItem():Item
		{
			return _selectedItem;
		}
		
		public function set selectedItem(value:Item):void
		{
			_selectedItem = value;
			dispatchEvent( new Event( "changeItem" ) );
		}
		
		//.................tempSubcategory......................
		private var _tempItem:Item=new Item();
		
		[Bindable(event="changeTempItem")]		
		public function get tempItem():Item
		{
			return _tempItem;
		}
		
		public function set tempItem(value:Item):void
		{
			_tempItem = value;
			dispatchEvent(new Event("changeTempItem"));
		}
		
		//--------------------------------------------------------------------
		//    Constructor
		//--------------------------------------------------------------------
		public function ItemCRUDModel(dispatcher:IEventDispatcher)
		{
			super(dispatcher);
		}
		//--------------------------------------------------------------------
		//    Public Methods
		//--------------------------------------------------------------------
		
		//.................selectItem......................
		override public function select(item:Object):void
		{
			super.select(item);
			updateCategory(item.subcategory.category);
			tempItem = item.clone();
			var event:ManagerEvent = new ManagerEvent( ManagerEvent.SELECT_ITEM );
			event.item = item as Item;
			dispatcher.dispatchEvent( event );
		}
		override public function edit(item:Object):void
		{
			super.edit(item);
			select(item);
			stateIndex = BaseCRUDModel.EDITING;
		}
		
		//.................deleteItem......................
		override public function remove():void
		{
			super.remove();
			var event:ManagerEvent = new ManagerEvent( ManagerEvent.DELETE_ITEM );
			event.item = selectedItem;
			dispatcher.dispatchEvent( event );
			unselect();
		}
		
		//.................addNewItem......................
		override public function addNew():void
		{
			super.addNew();
			var evet:ManagerEvent = new ManagerEvent(ManagerEvent.SAVE_ITEM);
			evet.item = tempItem;
			dispatcher.dispatchEvent(evet);
			unselect();
		}
		
		//.................updateItem......................
		override public function update():void
		{
			super.update();
			selectedItem.name=tempItem.name;
			selectedItem.icon=tempItem.icon;
			selectedItem.subcategory=tempItem.subcategory;
			selectedItem.subcategory.category=tempItem.subcategory.category;
			selectedItem.longDescription = tempItem.longDescription;
			selectedItem.shortDescription = tempItem.shortDescription;
			selectedItem.price = tempItem.price;
			selectedItem.ranking = tempItem.ranking;
			selectedItem.showOrder = tempItem.showOrder;
			var evet:ManagerEvent = new ManagerEvent(ManagerEvent.UPDATE_ITEM);
			evet.item = selectedItem;
			dispatcher.dispatchEvent(evet);
			unselect();
		}
		
		//.................unselectItem......................
		override public function unselect():void
		{
			super.unselect();
			var event:ManagerEvent = new ManagerEvent( ManagerEvent.SELECT_ITEM );
			event.item = new Item();
			dispatcher.dispatchEvent( event );
			selectedCategory=new Category();
			tempItem=new Item();
		}
		//.................cancel......................
		override public function cancel():void
		{
			super.cancel();
			selectedItem=new Item();
			tempItem=new Item();
			stateIndex=BaseCRUDModel.CREATING;
			
		}
		
		//--------------------------------------------------------------------
		//   update on change
		//--------------------------------------------------------------------
		
		public function updateItemName(value:String):void
		{
			tempItem.name=value;	
		}
		public function updateSelectedIcon(value:String):void
		{
			tempItem.icon=value;
		}
		public function updateShortDescription(value:String):void
		{
			tempItem.shortDescription=value;
		}
		public function updateLongDescription(value:String):void
		{
			tempItem.longDescription=value;
		}
		public function updateSubcategory(value:Subcategory):void
		{
			tempItem.subcategory=value;	
		}
		public function updatePrice(value:String):void
		{
			tempItem.price= parseFloat(value);
		}
		public function updateRanking(value:Number):void{
			tempItem.ranking=value;
		}
		public function updateShowOrder(value:int):void{
			tempItem.showOrder=value;
		}
		public function updateCategory(value:Category):void
		{
			selectedCategory = value;
			//update the filter and refresh
			subcategories.filterFunction = filterSubcategory;
			subcategories.refresh();
		}
		//--------------------------------------------------------------------
		//   Filters 
		//--------------------------------------------------------------------
		
		//.................filterSubcategory......................
		public function filterSubcategory(item:Object):Boolean
		{
			if (selectedCategory== null)
				return false;
			else
				return item.category.id == selectedCategory.id;
		}
		
		
		
	}
}